<!-- This document was created with HomeSite 2.5 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<HTML>
<!--
	THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
								J. Brook Monroe, mrprogguy@techie.com
    Copyright (c)2000 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

	Use at your own risk. No warranty is given or implied of the suitability 
	of this applet for any specific application. Neither Erica Sadun nor 
	Charles River Media will be held responsible for any unwanted effects 
	due to the use of this applet or any derivative.
-->	
<HEAD>
	<TITLE>Our Letters are Numbered</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
function charEval()
{
	var s = document.forms[0].input.value;
	if(s != '') {
		eval('document.forms[0].output.value='+s.charCodeAt(0));
	}
}

function makeChar()
{
	var s = document.forms[0].output.value;
	if(s != '') {
		var n = 0 + s;
		if(n > 0 && n < 256) {
			var sn = String.fromCharCode(n);
			eval('document.forms[0].alpha.value="'+sn+'"');
		}
	}
}
//-->
</SCRIPT>
<BODY>
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = LEFT>Our Letters are Numbered</H1></FONT>
<BLOCKQUOTE><FONT COLOR="770000">
In this recipe we look into the character codes behind the letters.
</FONT>        
<FORM>
Enter a letter here and press TAB: <INPUT TYPE="text" SIZE=10 NAME="input" onChange="charEval()"><BR>
See the character code here: <INPUT TYPE="text" SIZE=5 VALUE="" NAME="output" onChange="makeChar()"> or type a number to see the character here:
<INPUT TYPE="text" SIZE=2 NAME="alpha"><BR>
</FORM>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
All of the letters and digits you see on a Web page have numerical values called 'character codes.'
For example, the letter 'A' has the character code 65, and its lowercase counterpart 'a' has the character
code 97 (you can check this above).  The ability to move back and forth between these two representations
of a letter or digit can be very useful when writing JavaScript.  For example, in the Spreadsheet II recipe,
compatible with JavaScript 1.1 and lower, it was necessary to write the following bit of code to translate
a row designation to a number:
<FONT COLOR="770000" SIZE=3><PRE>
var CellID = "~ABCDEFGHIJKLMNOPQRSTUVWXYZ";
.
.
.
//--------------------------------------------------------------------------
// Converts cell alpha reference ([A]1,[B]2,[C]3...) into array index.
//--------------------------------------------------------------------------
function toCellIndex(f)
{
	f = f.toUpperCase();
	var n=CellID.indexOf(f);
	if(n == -1)
		n = 1;
	return n - 1;			
}
</PRE></FONT>
This is bulky and only partially portable.  With JavaScript 1.2 it would have been possible to write
<FONT COLOR="770000" SIZE=3><PRE>
function toCellIndex(cellRef) // cellRef is A1, B3, etc.
{
	return cellRef.charCodeAt(0) - 64;  // 1 less than the value of 'A'
}
</PRE></FONT>
The ability to translate numbers to characters is also useful in later recipes, when we write scripts that
can handle keyboard input.<p>
JavaScript 1.2 can also convert numbers to characters.  The <FONT COLOR="770000">String.fromCharCode()</FONT> function
takes any number of numeric arguments, and returns a string which is the concatentation of the characters
represented by the arguments.  Here's the result of
<FONT COLOR="770000">String.fromCharCode(67,65,84)</FONT>:'<SCRIPT LANGUAGE="JavaScript1.2">document.write(String.fromCharCode(67,65,84))</SCRIPT>'.
(Review the source to this page to make sure we aren't cheating!)<P>
Unless you're using an international version of a browser, there won't be character codes higher than 255 (the range is 0-255).
In actual practice, sending it 255 as an argument causes Netscape Communicator to toss up alert dialogs until it crashes
your computer--therefore, we don't recommend it.
</FONT>

<BR><BR><h5>Copyright ©2000 by Charles River Media, All Rights Reserved</h5>



</BODY>
</HTML>